<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\BaseController;
use app\common\controller\Backend;
use think\Exception;
use \think\facade\Cache;
use \app\admin\model\Category as CategoryModel;
use think\facade\Db;
use think\facade\View;
use tree\Tree;


class Category extends Backend
{
    public function initialize() {
        parent::initialize();
        $this->_mod  = new \app\admin\model\Category();
    }
    public function _before_index(){
        $list = $this->_mod->getTree(0);
        View::assign('list',$list);
    }

    public function _before_insert($data)
    {
        //检测分类是否存在
        if ($this->_mod->name_exists($data['name'], $data['pid'])) {
            return '分类名称已经存在';
        }
    }

    public function _handle_insert($data){
        //生成spid
        $data['spid'] = $this->_mod->get_spid($data['pid']);
        if ($data['spid'] == 0) {
            $data['spid'] = '0|';
        }
        if (substr_count($data['spid'], "|") == 3) {
            $data['spid'] = 'maxcate';
        }
        return $data;
    }

    public function _before_add(){
        $data = [];
        $pid = request()->get('pid')?request()->get('pid'):0;
        echo $pid;
        $catelist = $this->_mod->where(array('pid' => 0,'status' => 1))->select();
        foreach ($catelist as $k => $v) {
            $catelist[$k]['erji'] = $this->_mod->where(array('pid' => $v['id'],'status' => 1))->select();
        }
        $data['catelist']= $catelist;
        if ($pid) {
            $spid = $this->_mod->where(array('id' => $pid))->field('spid')->select();
            $spid = $spid ? $spid . $pid : $pid;
            $data['spid']= $spid;
        }
        $data['info']['pid']= $pid;
        View::assign($data);
    }

    public function _before_update($data){
        if ($this->_mod->name_exists($data['name'], $data['pid'], $data['id'])) {
            return '分类名称已经存在';
        }
        $old_pid = $this->_mod->field('img,pid')->where(array('id' => $data['id']))->find();
        if ($data['pid'] != $old_pid['pid']) {
            $count_pid = $this->_mod->field('id')->where(array('pid' => $data['id']))->count();
            if($count_pid>=1){
                return '有子分类，不能移动';
            }
            //不能把自己放到自己或者自己的子目录们下面
            $wp_spid_arr = $this->_mod->get_child_ids($data['id'], true);
            if (in_array($data['pid'], $wp_spid_arr)) {
                return '不能把自己放到自己或者自己的子目录们下面';
            }
        }
    }

    public function _handle_update($data){
        $old_pid = $this->_mod->field('img,pid')->where(array('id' => $data['id']))->find();
        if ($data['pid'] != $old_pid['pid']) {
            //重新生成spid
            $data['spid'] = $this->_mod->get_spid($data['pid']);
        }

        return $data;
    }

    public function _before_edit(){
        $data = [];
        $id = request()->param('id');
        $info = $this->_mod->where('id' , $id )->find();
        $data['info'] = $info;
        $catelist = $this->_mod->where(array('pid' => 0,'status' => 1))->select();
        foreach ($catelist as $k => $v) {
            $catelist[$k]['erji'] = $this->_mod->where(array('pid' => $v['id'],'status' => 1))->select();
        }
        $data['catelist']= $catelist;
        $pid = $info['pid'];
        if ($pid) {
            $spid = $this->_mod->where(array('id' => $pid))->field('spid')->select();
            $spid = $spid ? $spid . $pid : $pid;
            $data['spid']= $spid;
        }
        $data['pid']= $pid;
        View::assign($data);
    }


    public function _before_delete($ids) {
        $catecount = $this->_mod->whereIn('pid' ,$ids)->count();
        if ($catecount >= 1) {
            return '该分类下有子分类，不能删除！';
        }

        $concount = Db::name('doc_con')->whereIn('cid' ,$ids)->count();
        if ($concount >= 1) {
            return '分类下有 ' .$concount. ' 个文档，不能删除！';
        }
    }

}




